import {
    ADD_CART,
    ALL,
    UN_ALL,
    CHECK_ITEM,
    DEL_GOODS
} from './mutations-types.js'
import { Dialog } from 'vant';
import http from '@/common/api/request.js'
export default{
    state:{
        list:[],//保存购物车数据 
        checkedList:[]//选中的商品的 “id号”
    },
    getters:{
        checkAll( state ){
           return state.list.length == state.checkedList.length;
        },
        total( state ){
            let objTotal = {
                num:0,
                price:0
            }
            
            state.list.forEach(v=>{
                if( v.checked ){
                    objTotal.num += parseInt(v.goods_num);
                    objTotal.price += v.goods_num * v.goods_price;
                }
            })
            
            return objTotal;
        }
    },
    mutations:{
        [ADD_CART]( state , arr ){
            state.list = arr;
            
            state.checkedList = state.list.map(v=>{
                return v.id;
            })
        },
        //全选
        [ALL]( state ){
            //list 有东西，并且  list的length和checkedList的length 是一样的
            //list每一个对象checked必须值都是true
            state.checkedList = state.list.map(v=>{
               v.checked = true;
               return v.id;
            })
        },
        //全不选
        [UN_ALL]( state ){
            state.list.forEach(v=>{
                v.checked = false;
            })
            state.checkedList  = [];  
        },
        //单选
        [CHECK_ITEM]( state  , id ){
            
            //查找到  checkedList  中有没有此数据
            let index = state.checkedList.indexOf(id)
            
            if( index > -1 ){
                //有 ==》就删除掉
                return state.checkedList.splice( index , 1)
                
            }else{
                //没有 ==》就添加上
                state.checkedList.push( id );
            }
        },
        //删除方法 ==> 删除list数据中的内容
        [DEL_GOODS]( state ){
            state.list = state.list.filter(v=>{
                return state.checkedList.indexOf( v.id ) == -1
            })
        }
        
    },
    actions:{
        checkAllFn({commit,getters}){
            getters.checkAll ? commit('unAll') : commit('all');
        },
        delGoodsFn({commit,state},id){
            
            let arrCart = [];
            
            Dialog.confirm({
              message: '确定删除吗？ - - ！',
            }).then(() => {
                
                if( typeof id == 'number' ){
                    
                    arrCart = [id];
                    
                    let index = state.list.findIndex(v=>{
                        return v.id = id;
                    })
                    state.list.splice( index , 1 );
                    
                }else{
                    arrCart = state.checkedList;
                    commit('delGoods');
                }
                
                http.$axios(  {
                    url:'/api/deleteCart',
                    method:'post',
                    headers:{
                        token:true
                    },
                    data:{
                        arrId : arrCart //要传入商品的id号
                    }
                }).then(res=>{
                    console.log( res );
                })
            })
        }
    }
}